//
//  WebService.m
//  YYZB
//
//  Created by 顾悦 on 16/3/21.
//
//

#import "WebService.h"

#import "NetworkController.h"
#import "UserInfoController.h"
#import "Reachability.h"

#import "AppConfiguration.h"

@interface WebService ()

@property (nonatomic, strong) NetworkController *networkController;
@property (copy, nonatomic) NSString *webServiceURL;

@end

@implementation WebService

- (NetworkController *)networkController {
    if (!_networkController) {
        _networkController = NetworkController.new;
    }
    
    return _networkController;
}

- (NSString *)webServiceURL {
    return [AppConfiguration APIURLString];
}

- (void)sendHTTPGetRequest:(NSString *)URLString
         completionHandler:(Completion)completionHandler {
    [self.networkController getFromURL:URLString completionHandler:completionHandler];
}

- (NSString *)formattedURLString:(NSDictionary *)params method:(NSString *)method sessionNeeded:(BOOL)flag {
    NSDictionary *paramsDic = @{
                                @"callSource":@"3",
                                @"userId": [UserInfoController userID],
                                @"sessionKey": [UserInfoController sessionKey],
                                @"param": params
                                };
    NSData *paramsData = [NSJSONSerialization dataWithJSONObject:paramsDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *paramsString = [paramsData base64EncodedStringWithOptions:0];
    NSLog(@"%@ - %@", method, [[NSString alloc] initWithData:paramsData encoding:NSUTF8StringEncoding]);
    return [NSString stringWithFormat:@"%@%@?appParam=%@", self.webServiceURL, method, paramsString];
}

- (id)unboxedValue:(id)obj error:(NSError **)error {
    if ([obj isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = (NSDictionary *)obj;
        if ([result[@"resultCode"] integerValue] == 1) {
            error = nil;
            return result[@"resultValue"];
        } else {
            if ([result[@"resultCode"] integerValue] == -2) {
                *error = nil;//[NSError errorWithDomain:@"" code:-2 userInfo:@{NSLocalizedDescriptionKey: @""}];
                return nil;
            } else if (result[@"resultCode"] && result[@"resultMsg"]) {
                *error = [NSError errorWithDomain:@"" code:[result[@"resultCode"] integerValue] userInfo:@{NSLocalizedDescriptionKey: result[@"resultMsg"]}];
                if ([result[@"resultCode"] integerValue] == -20) {
                    [UserInfoController clearLoginInfo];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationNameUserLogout" object:nil];
                }

            } else {
                *error = [NSError errorWithDomain:@"" code:-99 userInfo:@{NSLocalizedDescriptionKey: @"未知错误"}];
            }
            return nil;
        }
    }
    return (nil, @"未知错误");
}

+ (BOOL)isNetworkAvailable {
    Reachability *r = [Reachability reachabilityForInternetConnection];
    return r.currentReachabilityStatus != NotReachable;
}

@end
